<?php

require('config.php');

$page = new rootLayoutCategory();
$page->setBaliseTitle('profil_add_balise');
$page->setDescription('profil_add_description');

// champ de recherche d'ingrédient
$page->addJavaScript('jQuery.bgiframe.min.js');
$page->addJavaScript('jQuery.Autocomplete-2.js');
$page->addStyleSheet('jQuery.Autocomplete.css');
$page->addJavaScript('tOne.ProfilModifier.js');
$page->addJavaScript('jQuery.calendar.inscription.js');
$page->addJavaScript('date_validate.js');
$page->addJavaScript('jQuery.form.validation.js');
$page->addJavaScript('keyEnable.js');
$page->addStyleSheet('jquery-calendar.css');
//-- menu Left show products--------------------------------//
$menu = new layoutMenu();
$menuCategorie = new layoutMenuCategorie();
$menu->pushMenu($menuCategorie);
$menuPage = new layoutMenuPage();
$menu->pushMenu($menuPage);
$page->setMenu($menu);


if(!isset($_SESSION['id_user'])) {
    $msg = new Message('Vous devez être connecté pour accéder à cette page', Message::AVERT);
    Persistence::register(PERSIST_MESSAGE, $msg);
    redirectFront('connexion');
}


if(isset($_POST['submit'])) {
    try {
    	
    	$nick_name = addslashes($_POST['pseudo']);
		$password = addslashes($_POST['password']);
		$birth_date_ok = addslashes($_POST['birth_date_ok']);
		$id_language = $_POST['language_default'];
        $id_infos_new = modelUser::insertUserinfos($nick_name,$password,$birth_date_ok);
            
        modelUser::linkNicknameLanguage($id_infos_new, $id_language,modelUser::LANG_DEFAULT);
    	    	
		modelUser::inscriptionInsertIDID($id_infos_new,$_SESSION['id_user']);

        $msg = new Message('Your new profile has been created', Message::INFO);
        Persistence::register(PERSIST_MESSAGE, $msg);
		redirectFront('profil');
    }
    catch(Exception $e) {
        $msg = new Message($e->getMessage(), Message::AVERT);
        Persistence::register(PERSIST_MESSAGE, $msg);
    }
}



//-- corps de la page --------------------------------------------------------//
$tpl_file='profil-add.tpl';
$profil_add = new flyLayout(REP_TPL . $tpl_file);
$profil_add->start();

$profil_add->setVariable('rep_ajax', REP_AJAX);
$tabs = new layoutTabs();
$tabs->pushLink('Mon compte', '/profil.php');
$tabs->pushLink('Mes messages', '/profil-message.php');
$profil_add->includeLayout('tabs', $tabs);

$profil_add->setVariable('delay_search', DELAY_SEARCH);//

$listProfile = modelUser::getListProfil($_SESSION['id_user']);
$listProfile->size()>0?$profil_add->showBlock('add').$profil_add->hideBlock('create'):$profil_add->hideBlock('add').$profil_add->showBlock('create');
	$listLanguage = modelLanguage::getList();
	if($listLanguage->size() > 0){
		foreach($listLanguage as $key => $language){
			$profil_add->setVariable('language', $language->name);
			$profil_add->setVariable('id_language', $language->id_language);
			$language->code==$_SESSION['language_code']?$profil_add->setVariable('selected', 'selected'):$profil_add->setVariable('selected', '');
			$profil_add->parseList('list-language');
		}
	}
$profil_add->stop();
$page->setBody($profil_add);

$page->display();

?>